package org.jaaksi.test.jpush.util;

import android.content.Context;
import android.content.SharedPreferences;
import org.jaaksi.test.jpush.model.bean.AgentInfo;
import org.jaaksi.test.jpush.model.push.MsgPushFeedBean;

/**
 * Created by Fcy on 2016/1/23. SharedPreferences
 */
public class SpFactory
{

	private static SpFactory mInstance = null;
	private SharedPreferences sp;

	private SharedPreferences.Editor editor;

	private static final String TOKEN = "token";
	private static final String AGENT_INFO = "agent_info";

	private static final String CUSTOMER = "customer";
	private static final String RENT = "customer";
	private static final String NEWHOUSE = "newhouse";
	/**
	 * key区分不同版本2.1.2版本
	 */
	private static final String LOGIN_INFO_KEY = "LOGIN_INFO_KEY_212";

	private SpFactory(Context context)
	{
		// this.context = context;
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		editor = sp.edit();
	}

	public static SpFactory getInstance(Context context)
	{
		if (mInstance == null)
		{
			mInstance = new SpFactory(context);
		}
		return mInstance;
	}

	public void setToken(String token)
	{
		editor.putString(TOKEN, token);
		editor.commit();
	}

	public String getToken()
	{
		return sp.getString(TOKEN, null);
	}

	public void setAgentInfo(AgentInfo agentInfo)
	{

		editor.putString(AGENT_INFO, DataUtil.toJsonString(agentInfo));
		editor.commit();
	}

	public AgentInfo getAgentInfo()
	{
		return DataUtil.getBean(sp.getString(AGENT_INFO, null),
		        AgentInfo.class);
	}

	/**
	 * 保存用户名和密码
	 *
	 * @param username
	 */
	public void setUsername(String username)
	{
		editor.putString("username", username);
		editor.commit();
	}

	public String getUsername()
	{
		return sp.getString("username", "");
	}

	public void setPassword(String password)
	{
		editor.putString("password", password);
		editor.commit();
	}

	public String getPassword()
	{
		return sp.getString("password", "");
	}

	public void setLogin(boolean isLogin)
	{
		editor.putBoolean("isLogin", isLogin);
		editor.commit();
	}

	public boolean isLogin()
	{
		return sp.getBoolean("isLogin", false);
	}

	// public void setCityCode(CityCodeBean cityCode)
	// {
	// editor.putString("cityCode", mDataUtil.mGson.toJson(cityCode));
	// editor.commit();
	// }
	//
	// public CityCodeBean getCityCode()
	// {
	// return mDataUtil.getData(sp.getString("cityCode", ""),
	// CityCodeBean.class);
	// }

	public void setHousePushFeedData(MsgPushFeedBean bean)
	{
		String uc_id = getAgentInfo() == null ? "" : getAgentInfo().id;
		String key = "house_pushFeed_" + uc_id;
		editor.putString(key, DataUtil.toJsonString(bean));
		editor.commit();
	}

	public MsgPushFeedBean getHousePushFeedData()
	{
		String key = getAgentInfo() == null ? "house_pushFeed_"
		        : "house_pushFeed_" + getAgentInfo().id;

		MsgPushFeedBean bean = DataUtil.getBean(sp.getString(key, null),
		        MsgPushFeedBean.class);

		return bean != null ? bean : new MsgPushFeedBean();
	}

	public void setCustomerPushFeedData(MsgPushFeedBean bean)
	{
		String uc_id = getAgentInfo() == null ? "" : getAgentInfo().id;
		String key = "customer_pushFeed_" + uc_id;
		editor.putString(key, DataUtil.toJsonString(bean));
		editor.commit();
	}

	public MsgPushFeedBean getCustomerPushFeedData()
	{
		String key = getAgentInfo() == null ? "customer_pushFeed_"
		        : "customer_pushFeed_" + getAgentInfo().id;

		MsgPushFeedBean bean = DataUtil.getBean(sp.getString(key, null),
		        MsgPushFeedBean.class);

		return bean != null ? bean : new MsgPushFeedBean();
	}

}
